﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace WebPALTT.UI
{
    public partial class OptionsDialog : Form
    {
        public OptionsDialog()
        {
            InitializeComponent();
            Init_ResultsGeneralPanel();
            Init_ResultsDatabasePanel();
            Init_ResultsHTMLPanel();

            LoadData();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count != 1)
                return;
            panel_template.Controls.Clear();
            switch (listView1.SelectedItems[0].Group.Name)
            {
                case "lvg_Environment":
                    switch (listView1.SelectedItems[0].Text)
                    {
                        case "General":
                            break;
                    }
                    break;
                case "lvg_Results":
                    switch (listView1.SelectedItems[0].Text)
                    {
                        case "General":
                            panel_template.Controls.Add(genresults_panel);
                            break;
                        case "HTML":
                            panel_template.Controls.Add(html_panel);
                            break;
                        case "Database":
                            panel_template.Controls.Add(database_panel);
                            break;
                    }
                    break;
            }
        }

        private void LoadData()
        {
            this.cb_res_gen_savetype.SelectedItem = Properties.Settings.Default.Results_Gen_SaveFormat;
            this.tb_res_gen_path.Text = Properties.Settings.Default.Results_Gen_TempPath;
            this.nud_res_gen_maxsaves.Value = Properties.Settings.Default.Results_Gen_MaxCount;

            this.tb_res_html_css.Text = Properties.Settings.Default.Results_HTML_css;

            this.tb_res_db_Server.Text = Properties.Settings.Default.Results_DB_Server;
            this.tb_res_db_Name.Text = Properties.Settings.Default.Results_DB_Name;
            this.cb_res_db_Intergrated.Checked = Properties.Settings.Default.Results_DB_UseIntergrate;
            this.tb_res_db_Uid.Text = Properties.Settings.Default.Results_DB_Uid;
            this.tb_res_db_Pwd.Text = Properties.Settings.Default.Results_DB_Pwd;
        }

        private void SaveData()
        {
            Properties.Settings.Default.Results_Gen_SaveFormat = (string)(this.cb_res_gen_savetype.SelectedItem ?? "");
            Properties.Settings.Default.Results_Gen_TempPath = this.tb_res_gen_path.Text;
            Properties.Settings.Default.Results_Gen_MaxCount = (int)this.nud_res_gen_maxsaves.Value;

            Properties.Settings.Default.Results_HTML_css = this.tb_res_html_css.Text;

            Properties.Settings.Default.Results_DB_Server = this.tb_res_db_Server.Text;
            Properties.Settings.Default.Results_DB_Name = this.tb_res_db_Name.Text;
            Properties.Settings.Default.Results_DB_UseIntergrate = this.cb_res_db_Intergrated.Checked;
            Properties.Settings.Default.Results_DB_Uid = this.tb_res_db_Uid.Text;
            Properties.Settings.Default.Results_DB_Pwd = this.tb_res_db_Pwd.Text;
            Properties.Settings.Default.Save();
        }

        #region Results General
        Panel genresults_panel = new Panel();
        Label lbl_res_gen_1 = new Label();
        Label lbl_res_gen_2 = new Label();
        Label lbl_res_gen_3 = new Label();
        ComboBox cb_res_gen_savetype = new ComboBox();
        TextBox tb_res_gen_path = new TextBox();
        Button b_res_gen_browse = new Button();
        NumericUpDown nud_res_gen_maxsaves = new NumericUpDown();

        private void Init_ResultsGeneralPanel()
        {
            this.lbl_res_gen_1.AutoSize = true;
            this.lbl_res_gen_1.Anchor = AnchorStyles.Left;
            this.lbl_res_gen_1.Text = "Default Save Type:";
            this.lbl_res_gen_2.AutoSize = true;
            this.lbl_res_gen_2.Anchor = AnchorStyles.Left;
            this.lbl_res_gen_2.Text = "Archive file path:";
            this.lbl_res_gen_3.AutoSize = true;
            this.lbl_res_gen_3.Anchor = AnchorStyles.Left;
            this.lbl_res_gen_3.Text = "Max files to archive:";

            this.cb_res_gen_savetype.Items.AddRange(new string[] {"File", "Database"});
            this.cb_res_gen_savetype.Name = "cb_res_gen_savetype";
            this.cb_res_gen_savetype.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.cb_res_gen_savetype.TabIndex = 1;

            this.tb_res_gen_path.Text = "";
            this.tb_res_gen_path.Name = "tb_res_gen_path";
            this.tb_res_gen_path.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.tb_res_gen_path.TabIndex = 2;

            this.b_res_gen_browse.Text = "browse...";
            this.b_res_gen_browse.Name = "b_res_gen_browse";
            this.b_res_gen_browse.Click += new EventHandler(b_res_gen_browse_Click);
            this.b_res_gen_browse.Anchor = AnchorStyles.Left;
            this.b_res_gen_browse.TabIndex = 3;

            this.nud_res_gen_maxsaves.Maximum = int.MaxValue;
            this.nud_res_gen_maxsaves.Minimum = 0;
            this.nud_res_gen_maxsaves.Name = "nud_res_gen_maxsaves";
            this.nud_res_gen_maxsaves.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.nud_res_gen_maxsaves.TabIndex = 4;

            TableLayoutPanel _tableLayoutPanel = new TableLayoutPanel();
            _tableLayoutPanel.AutoScroll = true;
            _tableLayoutPanel.AutoSize = true;
            _tableLayoutPanel.ColumnCount = 3;
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 75.0F));
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25.0F));
            _tableLayoutPanel.Controls.Add(lbl_res_gen_1, 0, 0);
            _tableLayoutPanel.Controls.Add(cb_res_gen_savetype, 1, 0);
            _tableLayoutPanel.Controls.Add(lbl_res_gen_2, 0, 1);
            _tableLayoutPanel.Controls.Add(tb_res_gen_path, 1, 1);
            _tableLayoutPanel.Controls.Add(b_res_gen_browse, 2, 1);
            _tableLayoutPanel.Controls.Add(lbl_res_gen_3, 0, 2);
            _tableLayoutPanel.Controls.Add(nud_res_gen_maxsaves, 1, 2);
            _tableLayoutPanel.Dock = DockStyle.Fill;
            _tableLayoutPanel.Location = new Point(0, 0);
            _tableLayoutPanel.Name = "_tableLayoutPanel";
            _tableLayoutPanel.RowCount = 4;
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.TabIndex = 0;

            genresults_panel.Controls.Add(_tableLayoutPanel);
            genresults_panel.Dock = DockStyle.Fill;
        }

        void b_res_gen_browse_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfdialog = new SaveFileDialog();
            sfdialog.Title = "Archive File Path";
            sfdialog.Filter = "Temp File|*.tmp|All files|*.*";
            if (sfdialog.ShowDialog(this) == DialogResult.OK)
            {
                tb_res_gen_path.Text = sfdialog.FileName;
            }
        }
        #endregion

        #region Results Database
        Panel database_panel = new Panel();
        Label lbl_res_db_1 = new Label();
        Label lbl_res_db_2 = new Label();
        Label lbl_res_db_3 = new Label();
        Label lbl_res_db_4 = new Label();
        Label lbl_res_db_5 = new Label();
        TextBox tb_res_db_Server = new TextBox();
        TextBox tb_res_db_Name = new TextBox();
        CheckBox cb_res_db_Intergrated = new CheckBox();
        TextBox tb_res_db_Uid = new TextBox();
        TextBox tb_res_db_Pwd = new TextBox();

        private void Init_ResultsDatabasePanel()
        {
            this.lbl_res_db_1.AutoSize = true;
            this.lbl_res_db_1.Anchor = AnchorStyles.Left;
            this.lbl_res_db_1.Text = "Server";
            this.lbl_res_db_2.AutoSize = true;
            this.lbl_res_db_2.Anchor = AnchorStyles.Left;
            this.lbl_res_db_2.Text = "Name";
            this.lbl_res_db_3.AutoSize = true;
            this.lbl_res_db_3.Anchor = AnchorStyles.Left;
            this.lbl_res_db_3.Text = "Use Intergrated";
            this.lbl_res_db_4.AutoSize = true;
            this.lbl_res_db_4.Anchor = AnchorStyles.Left;
            this.lbl_res_db_4.Text = "User Id";
            this.lbl_res_db_5.AutoSize = true;
            this.lbl_res_db_5.Anchor = AnchorStyles.Left;
            this.lbl_res_db_5.Text = "Password";

            this.tb_res_db_Server.Text = "";
            this.tb_res_db_Server.Name = "tb_res_db_Server";
            this.tb_res_db_Server.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.tb_res_db_Server.TabIndex = 1;

            this.tb_res_db_Name.Text = "";
            this.tb_res_db_Name.Name = "tb_res_db_Name";
            this.tb_res_db_Name.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.tb_res_db_Name.TabIndex = 2;

            this.cb_res_db_Intergrated.Text = "";
            this.cb_res_db_Intergrated.Checked = true;
            this.cb_res_db_Intergrated.Name = "cb_res_db_Intergrated";
            this.cb_res_db_Intergrated.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.cb_res_db_Intergrated.TabIndex = 3;

            this.tb_res_db_Uid.Text = "";
            this.tb_res_db_Uid.Name = "tb_res_db_Uid";
            this.tb_res_db_Uid.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.tb_res_db_Uid.TabIndex = 4;

            this.tb_res_db_Pwd.Text = "";
            this.tb_res_db_Pwd.Name = "tb_res_db_Pwd";
            this.tb_res_db_Pwd.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.tb_res_db_Pwd.TabIndex = 5;
            this.tb_res_db_Pwd.UseSystemPasswordChar = true;

            TableLayoutPanel _tableLayoutPanel = new TableLayoutPanel();
            _tableLayoutPanel.AutoScroll = true;
            _tableLayoutPanel.AutoSize = true;
            _tableLayoutPanel.ColumnCount = 2;
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle());
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle());            
            _tableLayoutPanel.Controls.Add(lbl_res_db_1, 0, 0);
            _tableLayoutPanel.Controls.Add(tb_res_db_Server, 1, 0);
            _tableLayoutPanel.Controls.Add(lbl_res_db_2, 0, 1);
            _tableLayoutPanel.Controls.Add(tb_res_db_Name, 1, 1);
            _tableLayoutPanel.Controls.Add(lbl_res_db_3, 0, 2);
            _tableLayoutPanel.Controls.Add(cb_res_db_Intergrated, 1, 2);
            _tableLayoutPanel.Controls.Add(lbl_res_db_4, 0, 3);
            _tableLayoutPanel.Controls.Add(tb_res_db_Uid, 1, 3);
            _tableLayoutPanel.Controls.Add(lbl_res_db_5, 0, 4);
            _tableLayoutPanel.Controls.Add(tb_res_db_Pwd, 1, 4);
            _tableLayoutPanel.Dock = DockStyle.Fill;
            _tableLayoutPanel.Location = new Point(0, 0);
            _tableLayoutPanel.Name = "_tableLayoutPanel";
            _tableLayoutPanel.RowCount = 6;
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.TabIndex = 0;

            database_panel.Controls.Add(_tableLayoutPanel);
            database_panel.Dock = DockStyle.Fill;
        }
        #endregion

        #region Results HTML
        Panel html_panel = new Panel();
        Label lbl_res_html_1 = new Label();
        TextBox tb_res_html_css = new TextBox();
        Button b_res_html_browse = new Button();

        private void Init_ResultsHTMLPanel()
        {
            this.lbl_res_html_1.AutoSize = true;
            this.lbl_res_html_1.Anchor = AnchorStyles.Left;
            this.lbl_res_html_1.Text = "CSS File:";

            this.tb_res_html_css.Text = "";
            this.tb_res_html_css.Name = "tb_res_html_css";
            this.tb_res_html_css.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.tb_res_html_css.TabIndex = 1;

            this.b_res_html_browse.Text = "browse...";
            this.b_res_html_browse.Name = "b_res_html_browse";
            this.b_res_html_browse.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.b_res_html_browse.Click += new EventHandler(b_res_html_browse_Click);
            this.b_res_html_browse.TabIndex = 2;

            TableLayoutPanel _tableLayoutPanel = new TableLayoutPanel();
            _tableLayoutPanel.AutoScroll = true;
            _tableLayoutPanel.AutoSize = true;
            _tableLayoutPanel.ColumnCount = 3;
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 75.0F));
            _tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25.0F));
            _tableLayoutPanel.Controls.Add(lbl_res_html_1, 0, 0);
            _tableLayoutPanel.Controls.Add(tb_res_html_css, 1, 0);
            _tableLayoutPanel.Controls.Add(b_res_html_browse, 2, 0);
            _tableLayoutPanel.Dock = DockStyle.Fill;
            _tableLayoutPanel.Location = new Point(0, 0);
            _tableLayoutPanel.Name = "_tableLayoutPanel";
            _tableLayoutPanel.RowCount = 2;
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.RowStyles.Add(new RowStyle());
            _tableLayoutPanel.TabIndex = 0;

            html_panel.Controls.Add(_tableLayoutPanel);
            html_panel.Dock = DockStyle.Fill;
        }

        void b_res_html_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdialog = new OpenFileDialog();
            ofdialog.Title = "CSS File Path";
            ofdialog.Filter = "CSS|*.css|All files|*.*";
            ofdialog.Multiselect = false;
            if (ofdialog.ShowDialog(this) == DialogResult.OK)
            {
                tb_res_html_css.Text = ofdialog.FileName;
            }
        }
        #endregion

        private void b_Save_Click(object sender, EventArgs e)
        {
            SaveData();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void b_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
